<?php

/**
 * Class LogviewController
 */
class LogviewController extends Controller{
    protected $configDataModel = null;
    public $tree_data = null;
    public $pk_m = null;
    /**
     * 前函数
     */
    public function init(){
         if(LoginHelper::getUserName() == '' || LoginHelper::getUserId() == 0){
             $this -> redirect($this -> _createUrl('login/index'));
         }
        return true;
     }

     public function indexAction(){
         $this -> display();
     }

    public function listAction(){
        $model = Controller::createMode('db_sys','t_sys_log_operate');
        $model_login = Controller::createMode('db_sys','t_sys_log_admin_login');
        $pagesize = 20;
        if($this->get('m') == 'view'){
            $page = $_GET['page']; // get the requested page
            if(!$page){
                $start = 0;
            }else{
                $start = ($page-1) * $pagesize;
            }
            $map = "user_id=".LoginHelper::getUserId();
            $sidx = $this->get('sidx');
            $sord = $this->get('sord');
            $searchOper = $this->get('searchOper');
            $searchString = $this->get('searchString');
            $searchField = $this->get('searchField');
            if($searchOper){
                $map .= " and {$searchField} like '%".$searchString."%' ";
            }
            if($sidx && $sord){
                $ord =  " {$sidx} {$sord} ";
            }else{
                $ord = "id asc";
            }
            $list = $model->where($map)->order($ord)->limit($start,$pagesize)->select();
            $count = $model->count('t_26_menu',1,$map);
            $responce = new stdClass();
            $responce->page = $page;
            $responce->total = round($count/$pagesize);
            $responce->records = $count;
            $i=0;
            foreach($list as $row) {
                $responce->rows[$i]['id']=$row['id'];
                $responce->rows[$i]['cell'] = array(
                    $row['id'],
                    $row['user_name'],
                    $row['action'],
                    $row['content'],
                    $row['create_time'],
                );
                $i++;
            }
            echo json_encode($responce);exit;
        } else {
            $this->assign('recent_udpate',$model->where(array('user_id'=>LoginHelper::getUserId()))->order("update_time desc")->limit(20)->select());
            $this->assign('login_record',$model_login->where(array('user_id'=>LoginHelper::getUserId()))->order("create_time desc")->limit(20)->select());
            $this -> display();
        }
    }
}
